﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using System.Data;

namespace WebUI.Controls
{
    public partial class SaleLookup : BaseEditControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                View_CategoryTree treeCategory = View_CategoryTree.GetCategoryTree();
                Utilities.LoadDropDownList(ddlCategory, treeCategory.DefaultView, View_CategoryTree.ColumnNames.Path, View_CategoryTree.ColumnNames.ID);

                Brands brand = Brands.LoadAllActiveBrands();
                brand.AddNew();
                brand.ID = 0;
                brand.BrandName = "--All--";
                brand.Sort = "ID asc";
                Utilities.LoadDropDownListNoSelectNoSort(ddlBrand, brand.DefaultView, Brands.ColumnNames.BrandName, Brands.ColumnNames.ID);

                RefreshProduct();
            }
        }

        public string ButtonText
        {
            get { return btnAdd.Text; }
            set { btnAdd.Text = value; }
        }

        protected void btnBarCode_OnClick(object sender, EventArgs e)
        {
            try
            {
                Products p = Products.GetProductByBarCode(txtBarCode.Text);
                if (p.RowCount > 0)
                {
                    ddlCategory.SelectedValue = p.s_CategoryID;
                    ddlBrand.SelectedValue = p.s_BrandID;
                    RefreshProduct();
                    if (ddlProduct.Items.Count > 1)
                    {
                        ddlProduct.SelectedValue = p.s_ID;
                        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "focusOnQuantity();", true);
                        txtQuantity.Focus();
                    }
                }
                else
                    ShowErrorMessage(mainPanel, "(*) Invalid Bar Code!");
            }
            catch (Exception ex)
            {
                ShowErrorMessage(mainPanel, ex.Message);
                ErrorLog.WriteToFile("SaleLookUp.ascx.cs", "btnBarCode_OnClick", ex.StackTrace);
            }
        }

        String IncludeAllChildCategories()
        {
            int from = ddlCategory.SelectedIndex;
            if (from < 0)
                return "";
            String res = ddlCategory.SelectedValue.ToString();
            String selectedPath = ddlCategory.SelectedItem.Text;
            for (int i = from; i < ddlCategory.Items.Count; i++)
            {
                if (ddlCategory.Items[i].Text.StartsWith(selectedPath + " - "))
                    res += "," + ddlCategory.Items[i].Value;
            }

            if (res.EndsWith(","))
                res = res.Remove(res.Length - 1);
            return res;
        }

        protected void ddlCategory_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshProduct();
        }

        protected void ddlBrand_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshProduct();
        }

        protected void ddlProduct_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlProduct.SelectedIndex > 0)
            {
                txtQuantity.Text = "1";
                btnAdd.Enabled = true;
                //txtBarCode.Text = Products.GetProductBarCode(int.Parse(ddlProduct.SelectedValue));
            }
            else
                btnAdd.Enabled = false;
        }

        void RefreshProduct()
        {
            if (ddlBrand.SelectedIndex >= 0 && ddlCategory.SelectedIndex > 0)
            {
                Products products = Products.GetNonIMEIProductByCategoriesBrand(IncludeAllChildCategories(), int.Parse(ddlBrand.SelectedValue));
                Utilities.LoadDropDownList(ddlProduct, products.DefaultView, Products.ColumnNames.ProductName, Products.ColumnNames.ID);
                if (ddlProduct.Items.Count > 0)
                {
                    ddlProduct.SelectedIndex = 0;
                }
            }
        }

        public delegate void AddProduct(object sender, ListItemEventArgs e);
        AddProduct onAddProduct;

        public AddProduct OnAddProduct
        {
            get { return onAddProduct; }
            set { onAddProduct = value; }
        }

        public delegate void AddIMEI(object sender, ListItemEventArgs e);
        AddIMEI onAddIMEI;

        public AddIMEI OnAddIMEI
        {
            get { return onAddIMEI; }
            set { onAddIMEI = value; }
        }


        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            if (onAddProduct != null && ddlProduct.SelectedIndex > 0)
            {
                ListItemEventArgs arg = new ListItemEventArgs();
                arg.Data = new System.Collections.Hashtable();
                arg.Data["ProductID"] = int.Parse(ddlProduct.SelectedValue);
                arg.Data["Quantity"] = int.Parse(txtQuantity.Text);

                onAddProduct(this, arg);
            }
        }

        protected void btnAddIMEI_OnClick(object sender, EventArgs e)
        {
            if (onAddIMEI != null)
            {
                if (ProductDetails.IsIMEIAvailable(txtIMEI.Text))
                {
                    ListItemEventArgs arg = new ListItemEventArgs();
                    arg.Data = new System.Collections.Hashtable();
                    arg.Data["IMEI"] = txtIMEI.Text;
                    onAddIMEI(this, arg);
                    txtIMEI.Text = "";
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("focusIMEI();", 1), true);
                }
                else
                    ShowErrorMessage(mainPanel, "(*) IMEI not available in stock!");
            }
        }
    }
}